let req = {};

/**
 * 设置http请求headers，添加token
 */
const setToken = () => {
    try {
        if (window.sessionStorage.getItem('token')) {
            if (req.headers.Authorization == null || req.headers.Authorization == "") {
                req.headers.Authorization = `bearer ${window.sessionStorage.getItem('token')}`;
            }
        } else {
            req.headers.Authorization = `Basic dHBmOnRwZg==`;
        }
    } catch (e) {
        console.log(e);
    }
};

const setHeaders = () => {
    setToken();
};

/**
 * 给请求的url添加随机参数，避免在IE10下本地缓存的问题
 */
const setRParams = () => {
    try {
        let joinStr = '?r=';
        if (req.url.indexOf('&') > -1) {
            joinStr = '&r=';
        }
        joinStr += new Date().getTime();
        // mock模式下不添加随机参数
        if (req.url.indexOf('http://') < 0) {
            joinStr = '';
        }
        req.url += joinStr;
    } catch (e) {
        console.log(e);
    }
};

export default {
    setHeader: (request) => {
        req = request || {};
        setHeaders();
        if (request.method === 'get') {
            setRParams();
        }
    }
};
